#!/bin/bash

BASE_PATH=$(sh toolkit/read-constant-from-file.sh .env "BASE_PATH")
SYSBENCH_RESULT=$(sh toolkit/read-constant-from-file.sh .env "SYSBENCH_RESULT")
SYSBENCH_GRAPH=$(sh toolkit/read-constant-from-file.sh .env "SYSBENCH_GRAPH")
SHARDING=$(sh toolkit/read-constant-from-file.sh .env "SHARDING")
READWRITE_SPLITTING=$(sh toolkit/read-constant-from-file.sh .env "READWRITE_SPLITTING")
SHADOW=$(sh toolkit/read-constant-from-file.sh .env "SHADOW")
ENCRYPT=$(sh toolkit/read-constant-from-file.sh .env "ENCRYPT")
SYSBENCH_GRAPH_PYTHON_FILE=$(sh toolkit/read-constant-from-file.sh .env "SYSBENCH_GRAPH_PYTHON_FILE")
SYSBENCH_MYSQL_SCRIPT=$(sh toolkit/read-constant-from-file.sh .env "SYSBENCH_PROXY_MYSQL_SCRIPT")
SYSBENCH_TEST_FUNCTION=$(sh toolkit/read-constant-from-file.sh .env "SYSBENCH_TEST_FUNCTION")
DATABASE_TYPE=$(sh toolkit/read-constant-from-file.sh .env "PROXY_MYSQL")
FOLDER_COUNT=$(sh toolkit/read-constant-from-file.sh .env "FOLDER_COUNT")
BUILD_NUMBER_FILE=$(sh toolkit/read-constant-from-file.sh .env "BUILD_NUMBER_FILE")

echo "${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}"
CURRENT_PATH=`pwd`

if [ ! -d "${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}" ]; then
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHARDING}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${READWRITE_SPLITTING}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHADOW}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${ENCRYPT}

  mkdir ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHARDING}/{1..15}
  mkdir ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${READWRITE_SPLITTING}/{1..15}
  mkdir ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHADOW}/{1..15}
  mkdir ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${ENCRYPT}/{1..15}


  cp toolkit/${SYSBENCH_GRAPH_PYTHON_FILE} ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH_PYTHON_FILE}
  chmod +x ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH_PYTHON_FILE}

  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH}/${SHARDING}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH}/${READWRITE_SPLITTING}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH}/${SHADOW}
  mkdir -p ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SYSBENCH_GRAPH}/${ENCRYPT}

  touch ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHARDING}/${BUILD_NUMBER_FILE}
  touch ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${READWRITE_SPLITTING}/${BUILD_NUMBER_FILE}
  touch ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHADOW}/${BUILD_NUMBER_FILE}
  touch ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${ENCRYPT}/${BUILD_NUMBER_FILE}

  ls -v ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHARDING}/ | tail -n14 > ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHARDING}/${BUILD_NUMBER_FILE}
  ls -v ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${READWRITE_SPLITTING}/ | tail -n14 > ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${READWRITE_SPLITTING}/${BUILD_NUMBER_FILE}
  ls -v ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHADOW}/ | tail -n14 > ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${SHADOW}/${BUILD_NUMBER_FILE}
  ls -v ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${ENCRYPT}/ | tail -n14 > ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_RESULT}/${ENCRYPT}/${BUILD_NUMBER_FILE}
fi

if [ ! -f "${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_MYSQL_SCRIPT}" ]; then
  cp sysbench/${DATABASE_TYPE}/${SYSBENCH_MYSQL_SCRIPT} ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_MYSQL_SCRIPT}
  cp sysbench/${DATABASE_TYPE}/${SYSBENCH_TEST_FUNCTION} ${BASE_PATH}/${DATABASE_TYPE}/${SYSBENCH_TEST_FUNCTION}
fi